<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

class JFX_Module_Blockstyle_Property_Base
{
	public $id = 'base';
	public $cssName = '';
	public $instanceId = '';
	public $elementId = '';
	public $title = 'Base';
	public $widgets = array();
	public $privateProperties = array('widgets', 'id', 'privateProperties');
	public $runtimeProperties = array();
	public $open = 0;

	public function __construct($element, $instanceId){
		$this->instanceId = $instanceId;
		$this->elementId = $element->id;
		$this->init();
	}
	public function hasWidget($widgetInstanceId){
		$widget = $this->getWidget($widgetInstanceId);
		if(is_object($widget) && $widget instanceof JFX_Module_Blockstyle_Widget_Base){
			return true;
		}
		return false;
	}

	public function hasEnabledWidget(){
		foreach($this->widgets as $k=>$widget){
			if($widget->enabled>0){
				return true;
			}
		}
		return false;
	}

	public function removeWidget($widgetInstanceId){
		if($this->hasWidget($widgetInstanceId)){
			foreach($this->widgets as $k=>$widget){
				if($widget->instanceId==$widgetInstanceId){
					unset($this->widgets[$k]);
				}
			}
		}
		$this->widgets = array_regenerate_keys($this->widgets);
	}

	public function getCssValue(){
		if(count($this->widgets)==0){
			return;
		}
		return $this->widgets[0]->value.$this->widgets[0]->unit;
	}

	public function __toCss(){
		$outputString = '';

		if(!$this->hasEnabledWidget()){
			return $outputString;
		}

		if(!$this->hasEnabledWidget() || $this->cssName=='' || $this->cssName != $this->id){
			return '/*not enabled '.$this->cssName.'*/ '.$outputString;
		}

		$cssValue = $this->getCssValue();
		if(trim($cssValue)==''){
			return $outputString;
		}

		$outputString .= '	'.$this->cssName.': '.$cssValue.';
';

		return $outputString;
	}

	public function getWidget($widgetInstanceId){
		foreach($this->widgets as $k=>$widget){
			if($widgetInstanceId == $widget->instanceId){
				return $this->widgets[$k];
			}
		}
	}

	public function updateFromJson($property){

		foreach($property->widgets as $k=>$widget){
			if($this->hasWidget($widget->instanceId)){
				$this->getWidget($widget->instanceId)->updateFromJson($widget);
			}
		}

		foreach(get_object_vars($property) as $k=>$v){
			if(isset($this->$k)){

				if(in_array($k, $this->privateProperties)){
					continue;
				}

				if(is_object($v)){
					JFX::addError(__FILE__.__LINE__.' is object'.get_class($v));
					continue;
				}


				if($k!=='widgets'){
					$this->setProperty($k, $v);
				}
				
			}
		}
	}

	public function setProperty($name, $value){
		if(!in_array($name, $this->privateProperties)){
			$this->$name = $value;
		}
	}

	public function setWidgetProperty($widgetInstanceId, $name, $value){
		if($this->hasWidget($widgetInstanceId)){
			$this->getWidget($widgetInstanceId)->setProperty($name, $value);
		}
	}

	public function prepareJson(){
		foreach($this->runtimeProperties as $propertyName){
			if(isset($this->$propertyName)){
				$this->propertyName = null;
			}
		}
		foreach($this->widgets as $k=>$widget){
			$this->widgets[$k]->prepareJson();
		}
	}



}


